// Settings.cpp : implementation file
//

#include "stdafx.h"
#include "Settings.h"
#include "globals.h"
#include "afxdialogex.h"


// CSettings dialog

IMPLEMENT_DYNAMIC(CSettings, CDialogEx)

CSettings::CSettings(CWnd* pParent /*=NULL*/)
	: CDialogEx(CSettings::IDD, pParent)
{

}

CSettings::~CSettings()
{
}

void CSettings::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//  DDX_Control(pDX, IDC_COLORS, m_ComboColors);
	DDX_Control(pDX, IDC_COMBO_SOUND, m_PlaySound);
	//  DDX_Control(pDX, IDC_COMBO_BGIMAGE, m_BgImage);
	DDX_Control(pDX, IDC_COMBO_START_SOUND, m_PlayStartSound);
}


BEGIN_MESSAGE_MAP(CSettings, CDialogEx)
//	ON_CBN_SELCHANGE(IDC_COLORS, &CSettings::OnSelchangeColors)
	ON_CBN_SELCHANGE(IDC_COMBO_SOUND, &CSettings::OnSelchangeComboSound)
	ON_BN_CLICKED(IDOK, &CSettings::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CSettings::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON_BGCOLOR, &CSettings::OnClickedButtonBgcolor)
	ON_BN_CLICKED(IDC_BUTTON_DEFAULT, &CSettings::OnClickedButtonDefault)
//	ON_CBN_SELCHANGE(IDC_COMBO_BGIMAGE, &CSettings::OnSelchangeComboBgimage)
ON_CBN_SELCHANGE(IDC_COMBO_START_SOUND, &CSettings::OnSelchangeComboStartSound)
END_MESSAGE_MAP()


// CSettings message handlers


//void CSettings::OnSelchangeColors()
//{
//	UpdateData(TRUE);
//	
//}

#include "mmsystem.h"
void CSettings::OnSelchangeComboSound()
{
	UpdateData(TRUE);
	int play = m_PlaySound.GetCurSel();
	if (play > 0)
	{
		if (play == 1)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE1),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
		else if (play == 2)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE2),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
		else if (play == 3)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE_BELLS_ASCEND),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
		else if (play == 4)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE_TADA),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
		else if (play == 5)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE_MAGIC),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
	}
}


void CSettings::OnSelchangeComboStartSound()
{
	UpdateData(TRUE);
	int play = m_PlayStartSound.GetCurSel();
	if (play > 0)
	{
		if (play == 1)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE1),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
		else if (play == 2)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE2),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
		else if (play == 3)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE_BELLS_ASCEND),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
		else if (play == 4)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE_TADA),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
		else if (play == 5)
			PlaySound(MAKEINTRESOURCE(IDR_WAVE_MAGIC),GetModuleHandle(NULL), SND_RESOURCE|SND_ASYNC);
	}
}

BOOL CSettings::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	CDialogEx::SetBackgroundColor(GetInt("BackgroundColor", RGB(230,238,235)), TRUE);
	//m_ComboColors.SetCurSel(GetInt("Colors", 1));
	m_PlaySound.SetCurSel(GetInt("Sound", 3));
	m_PlayStartSound.SetCurSel(GetInt("SoundStartup", 5));
	m_bgcolor = GetInt("BackgroundColor", RGB(230,238,235));
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

COLORREF CSettings::ChooseAColor(COLORREF currentcolor)
{
	HBRUSH hbrush;                  // brush handle
	CHOOSECOLOR cc;                 // common dialog box structure 
	static COLORREF acrCustClr[16]; // array of custom colors 
	DWORD rgbCurrent = (DWORD)currentcolor; 
	// Initialize CHOOSECOLOR 
	ZeroMemory(&cc, sizeof(CHOOSECOLOR));
	cc.lStructSize = sizeof(CHOOSECOLOR);
	cc.hwndOwner = this->m_hWnd;
	cc.lpCustColors = (LPDWORD) acrCustClr;
	cc.rgbResult = rgbCurrent;
	cc.Flags = CC_FULLOPEN | CC_RGBINIT;
 
	if (ChooseColor(&cc)==TRUE) 
	{
		hbrush = CreateSolidBrush(cc.rgbResult);
		if (cc.rgbResult == 0xFF00FF)
			cc.rgbResult = 0xFF01FF;
		return cc.rgbResult; 
	}
	else return currentcolor;
}

void CSettings::OnBnClickedOk()
{
	WriteInt("Sound", m_PlaySound.GetCurSel());;
	WriteInt("SoundStartup", m_PlayStartSound.GetCurSel());
	WriteInt("BackgroundColor", m_bgcolor);
	//WriteInt("Colors", m_ComboColors.GetCurSel());
	CDialogEx::OnOK();
}

void CSettings::OnBnClickedCancel()
{
	CDialogEx::OnCancel();
}

void CSettings::OnClickedButtonBgcolor()
{
	m_newcolor = ChooseAColor(m_bgcolor);
	m_bgcolor = m_newcolor;
	UpdateData(FALSE);
}

void CSettings::OnClickedButtonDefault()
{
	m_PlaySound.SetCurSel(2);
	m_PlayStartSound.SetCurSel(5);
	m_bgcolor = RGB(255,255,255);
	UpdateData(FALSE);
}


